﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using omsData;
namespace myomscl_web.Admin.UserControls
{
    public partial class ucAddNewOrganization : System.Web.UI.UserControl
    {
        public string UserTypeID { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (OmsModelContainer context = new OmsModelContainer())
            {
                if (!context.Organisations.Include("UserType").Where(o=>o.UserType.Id.ToString()==UserTypeID&&
                                                                    o.Name==txtNewOrganizationName.Text).Any())
                {
                    var newOrganization = new Organisation()
                    {
                        Name=txtNewOrganizationName.Text,
                        City = txtCity.Text,
                        Address = txtAddress.Text,
                        Phone1 = txtPhone1.Text,
                        Phone2 = txtPhone2.Text,
                        Email = txtEmail.Text,
                        
                    };
                    

                    context.AddToOrganisations(newOrganization);
                    context.UserTypes.Where(ut => ut.Id.ToString() == UserTypeID).First().Organisations.Add(newOrganization);
                    context.SaveChanges();
                    AddedNewOrganization(newOrganization.Name);
                    
                }
            }
        }

        

        public delegate void OnAddedNewOrganization(string newOrganizationName);
        public event OnAddedNewOrganization AddedNewOrganization;
    }
}